package com.md.value.enums;

import com.md.base.EnumBase;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 异常规则: <p/>在一个 module 下 code 递增(1),不同 module 下 code 递增(10000)
 *
 * @author 刘骄阳
 * 2022-09-21 21:35
 */
@Getter
@AllArgsConstructor
public enum ErrorEnum implements EnumBase {
  SUCCESS(200, "成功!"),
  PERMISSION(403, "权限不足!"),
  UNAUTHORIZED(401, "登录过期!"),
  UN_EXISTENT(404, "地址不存在!"),
  SYSTEM_ERROR(500, "服务异常!"),
  COPY_ERROR(700, "拷贝异常!"),
  IS_EMPTY(701, "%s不能为空!"),
  READ_FILE_ERROR(702, "读取文件异常!"),
  WRITE_FILE_ERROR(703, "写入文件异常!"),
  SERIALIZE_ERROR(704, "序列化异常!"),
  BEAN_REGISTER_ERROR(705, "bean注入有误! %s"),
  SERIALIZE_PINYIN_ERROR(706, "拼音解析异常!"),
  ENCODING_ERROR(707, "加密异常!"),
  DECODING_ERROR(708, "解密异常!"),
  UPLOAD_FILE_ERROR(709, "上传文件异常!"),
  SNOW_FLAKE_CREATE_ERROR(710, "唯一id生成异常!"),
  REQUEST_LOCK_FAILED(711, "获取锁失败"),
  SQL_EXECUTE_ERROR(712, "SQL执行失败"),
  REDIS_SET_ERROR(713, "redis写入失败"),
  SMS_SEND_ERROR(30003, "短信发送失败!");

  private static final Map<Integer, ErrorEnum> ENUM_MAP = Arrays.stream(ErrorEnum.values())
    .collect(Collectors.toMap(EnumBase::getCode, e -> e));
  private final Integer code;
  private final String message;

  public static ErrorEnum of(final Integer code) {
    return ENUM_MAP.get(code);
  }
}
